﻿namespace LogManager.Utils.Unity
{
    using LogManager.Utils.ChangeTracking;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using System;
    using System.Collections.Generic;

    public class PropertyChangedBehavior : IInterceptionBehavior
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var nextDel = getNext();
            var result = nextDel(input, getNext);

            if (input.Target is PropertyChangeBase)
            {
                if (input.MethodBase.Name.StartsWith("set_"))
                {
                    var propertyName = input.MethodBase.Name.Substring(4);
                    var obj = (PropertyChangeBase)input.Target;
                    obj.RaisePropertyChanged(propertyName);
                }
            }

            return result;
        }

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        public bool WillExecute
        {
            get
            {
                return true;
            }
        }
    }
}
